package com.eye.catcher.utils;

public class HtmlUtil {
	public static String getSummary(String html, int length) {
		if (html == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer(html);

		int nIndexLeft = sb.toString().indexOf('<');
		while (nIndexLeft > -1) {
			sb = sb.delete(nIndexLeft, sb.toString().indexOf('>', nIndexLeft) + 1);
			nIndexLeft = sb.toString().indexOf('<', nIndexLeft);
		}
		int nIndexSpace = sb.toString().indexOf("&nbsp;");
		while ((nIndexSpace > -1) && (nIndexSpace < length * 4)) {
			sb = sb.delete(nIndexSpace, nIndexSpace + 6);
			nIndexSpace = sb.toString().indexOf("&nbsp;", nIndexSpace);
		}
		int nIndexNewLine = sb.toString().indexOf('\n');
		while ((nIndexNewLine > -1) && (nIndexNewLine < length * 3)) {
			sb = sb.delete(nIndexNewLine, nIndexNewLine + 1);
			nIndexNewLine = sb.toString().indexOf('\n', nIndexNewLine);
		}
		int nIndexReturn = sb.toString().indexOf('\r');
		while ((nIndexReturn > -1) && (nIndexReturn < length * 2 + 1)) {
			sb = sb.delete(nIndexReturn, nIndexReturn + 1);
			nIndexReturn = sb.toString().indexOf('\r', nIndexReturn);
		}
		int nLen = length * 2 + 1 < sb.length() ? length * 2 + 1 : sb.length();
		float nEndIndex = 0.0F;
		char c = 'a';
		if (nLen > length) {
			for (int i = 0; (i <= nEndIndex + length) && (nEndIndex + length < nLen); i++) {
				c = sb.charAt(i);
				if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
					nEndIndex += 0.5F;
				}
			}
		}
		nEndIndex = nEndIndex + length < nLen ? nEndIndex + length : nLen;
		return sb.substring(0, (int) nEndIndex);
	}
}
